package com.wl.api.common.utils;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;

import com.aliyun.api.gateway.demo.constant.ContentType;
import com.aliyun.api.gateway.demo.constant.HttpHeader_ali;
import com.aliyun.api.gateway.demo.constant.HttpSchema;
import com.tool.http.httpclient.HttpClientUtil;
import com.tool.util.http.common.HttpConfig;
import com.tool.util.http.common.HttpHeader;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

@SuppressWarnings("restriction")
public class BASE64Util {

    /**
     * base 64 encode
     * 
     * @param bytes
     *            待编码的byte[]
     * @return 编码后的base 64 code
     */
    public static String base64Encode(byte[] bytes) {
        return new BASE64Encoder().encode(bytes);
    }

    /**
     * base 64 decode
     * 
     * @param base64Code
     *            待解码的base 64 code
     * @return 解码后的byte[]
     * @throws Exception
     */
    public static byte[] base64Decode(String base64Code) throws Exception {
        return new BASE64Decoder().decodeBuffer(base64Code);
    }
    public static void main(String[] args) throws Exception {
	String a=new String("LOGISITCS:roadage2017515"); //账户 : 密码
	System.out.println(base64Encode(a.getBytes()));
	String base64Str = BASE64Util.base64Encode(a.getBytes());
	System.out.println("Base64解码后：" + new String(BASE64Util.base64Decode(base64Str)));
        URL url = new URL("https://api.dpd.co.uk/user?action=login");
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        String author = "Basic " +base64Encode(a.getBytes());
        conn.setRequestProperty("Authorization", author);
        conn.setRequestProperty("content-type", "application/json");
        conn.setRequestMethod("POST");
        conn.connect();
        System.out.println(conn.getResponseCode());
        System.out.println(conn.getContent());


	}
    
    public static void demo () throws Exception {
    	
    	 /**
         * HttpConfig  config = HttpConfig.custom();
         HttpHeader hheader = HttpHeader.custom();

         Map<String, String> headersMap = new HashMap<String, String>();
         Map<String, Object> contentMap = new HashMap<String, Object>();
         headersMap.put(HttpHeader_ali.HTTP_HEADER_ACCEPT, "application/json");
         headersMap.put(HttpHeader_ali.HTTP_HEADER_CONTENT_MD5, MessageDigestUtil.base64AndMD5(body));
         headersMap.put(HttpHeader_ali.HTTP_HEADER_CONTENT_TYPE, ContentType.CONTENT_TYPE_TEXT);

         headersMap = HttpUtil.initialBasicHeader(headersMap, OCR_APP_KEY, OCR_APP_SECRET, HttpMethod.POST, HttpSchema.HTTPS + OCR_HOST + url, null, CUSTOM_HEADERS_TO_SIGN_PREFIX);
         for (Map.Entry<String, String> e : headersMap.entrySet()) {
         hheader = hheader.other(e.getKey(), MessageDigestUtil.utf8ToIso88591(e.getValue()));
         }

         contentMap.put(Utils.ENTITY_STRING, body);
         */
        String a= "LOGISITCS:roadage2017515";
        String author = "Basic " + BASE64Util.base64Encode(a.getBytes());
        HttpConfig config = HttpConfig.custom();
        HttpHeader hheader = HttpHeader.custom();
        Map<String, Object> contentMap = new HashMap<>();
        hheader.other(HttpHeader_ali.HTTP_HEADER_ACCEPT, "application/json");
        hheader.other(HttpHeader_ali.HTTP_HEADER_CONTENT_TYPE, ContentType.CONTENT_TYPE_JSON);
        hheader.other("Authorization", author);
        HttpResponse response = HttpClientUtil.postWithHttpResponse(config.url(HttpSchema.HTTPS + "api.dpd.co.uk/user?action=login").headers(hheader.build()).paramap(contentMap));
        System.out.println(IOUtils.toString(response.getEntity().getContent()));
    }
}